import java.awt.* ;
import java.awt.event.* ;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.* ;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class GUI extends JFrame implements ActionListener, ListSelectionListener, WindowListener {
	private JPanel contentPanel;
	private JLabel titleLabel;
	private LogInOut logInOut;
	private OptionSelectionDialog optSelect;
	private FindRestaurantDialog findRestaurant;
	private AddRestaurantDialog addRestaurant;
	private EditRestaurantDialog editRestaurant;
	private DeleteRestaurantDialog deleteRestaurant;
	private CreateUserDialog createUser;
	private UserProfilesDialog userProfiles;
	private SearchResultsDialog searchResults;
	private DomainLogic dL;
	private ErrorMessageDialog errMessage;
	private boolean userLogged = false;
	private boolean adminLogged = false;
	Vector<Restaurant> v = new Vector<Restaurant>();

 
	//create rows
	JPanel emptyPanel = new JPanel();
	JPanel row = new JPanel();
    JPanel row2 = new JPanel();
    JPanel logInOutPanel = new JPanel();
	//JPanel displayOptionSelected = new JPanel();
	JPanel row3 = new JPanel();

	
	public GUI (DomainLogic domainLogic){
		dL = domainLogic;
		//contentPanel = (JPanel)getContentPane();
		getContentPane().setLayout(new GridLayout(1,0)); 
		
		this.addWindowListener(this);

	    row.setLayout(new GridLayout(0,1));
        row2.setLayout(new GridLayout(0,1));
        row3.setLayout(new BoxLayout(row3, BoxLayout.X_AXIS));
        //displayOptionSelected.setLayout(new GridLayout(0,1));
        //findRestaurantRow.setLayout(new BoxLayout(findRestaurantRow, BoxLayout.Y_AXIS));
        //row3.add(findRestaurantRow);
        //row3.add(searchResults);
        //row.setLayout(new GridLayout(1,0));
		
        // The restaurant finder label
		Font bigFont = new Font("SansSerif", Font.BOLD, 30);

		titleLabel = new JLabel("The Restaurant Finder");
		titleLabel.setFont(bigFont);
		//addElement(contentPanel, titleLabel,200,0,500,50);		
		//row.add(titleLabel);
		
		setTitle("Restaurant Finder Application");
		setSize(500, 400); // The GUI dimensions
		setLocation(new Point(150, 150)); //The GUI position
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setVisible(true);
		
		logInOut =  new LogInOut(dL);
		logInOut.getJButton().addActionListener(this);
		logInOut.getRButton().addActionListener(this);
		logInOutPanel.add(logInOut);

		//row.add(logInOutPanel);		
		//row.setMaximumSize(new Dimension(100,100));
		//row.setMinimumSize(new Dimension(100,100));

		//getContentPane().add(row);
		
		

		
		optSelect = new OptionSelectionDialog(userLogged, adminLogged);
		optSelect.findRestaurantButton().addActionListener(this);
		optSelect.savedProfilesButton().addActionListener(this);
		optSelect.addRestaurantButton().addActionListener(this);
		optSelect.editRestaurantButton().addActionListener(this);
		optSelect.deleteRestaurantButton().addActionListener(this);
		//row2.add(optSelect);
		
		
		findRestaurant = new FindRestaurantDialog();
		findRestaurant.getSearchButton().addActionListener(this);
		//row3.add(findRestaurant);
		findRestaurant.setVisible(false);
		

	
		

		//findRestaurantRow.add(findRestaurant);
		//findRestaurantRow.setVisible(false);
		
	//	searchResults = new SearchResultsDialog();
		//searchResults.setVisible(false);
		
		//search results
		
		JPanel myPanel = new JPanel();
		myPanel.setLayout(new GridLayout(0,1));
		
		myPanel.add(row);
		//myPanel.add(row2);
		//myPanel.add(findRestaurant);
		
		//myPanel.add(findRestaurantRow);
		//this.add(titleLabel);
		row.add(logInOut);
		row.add(emptyPanel);
		row.add(optSelect);
		row.add(emptyPanel);
		this.add(row);
		this.add(findRestaurant);
		//this.add(searchResults);
		pack();
		

	}
		
	public void addElement (Container c, Component w, int x, int y, int h, int i) {
		w.setBounds(x, y, h, i);
		c.add(w);
		}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() != null) {		
			if( e.getSource().equals(logInOut.getJButton())){
				// log in
				if( logInOut.getJButton().getText().equals("Sign in")){
					// send sign in to DL HERE

					if(logInOut.signIn()){
						if(dL.getUser().getType().equals("admin")){  	
							adminLogged = true;
							addRestaurant = new AddRestaurantDialog();
							editRestaurant = new EditRestaurantDialog();
							deleteRestaurant = new DeleteRestaurantDialog();
						}
						else{
							userLogged = true;	
						}
					}
					if(logInOut.signIn()){

						optSelect.showSavedProfileOption(userLogged);
						optSelect.showAddRestaurantOption(adminLogged);
						optSelect.showEditRestaurantOption(adminLogged);
						optSelect.showDeleteRestaurantOption(adminLogged);
					}
				}
				
				//log out
				else if( logInOut.getJButton().getText().equals("Sign out")){
					userLogged = false;
					adminLogged = false;
					optSelect.showSavedProfileOption(userLogged);
					optSelect.showAddRestaurantOption(userLogged);
					optSelect.showEditRestaurantOption(userLogged);
					optSelect.showDeleteRestaurantOption(userLogged);
					logInOut.signOut();
					logInOut.repaint();
				}		
			}
			
			else if (e.getSource().equals(logInOut.getRButton())) {
				createUser = new CreateUserDialog(dL);
				createUser.setVisible(true);
			}
			
			// option Selection
			else if (e.getSource().equals(optSelect.findRestaurantButton())){
				//displayOptionSelected.removeAll();
				findRestaurant.setVisible(true);
			}		
			
			else if (e.getSource().equals(optSelect.savedProfilesButton())){
				userProfiles = new UserProfilesDialog(dL.getUserPreferences(), dL);
				userProfiles.setVisible(true);	
			//	displayOptionSelected.add(userProfiles);
				//userProfiles.setVisible(true);
				//this.add(displayOptionSelected);
				//displayOptionSelected.setVisible(true);
			}
			
			else if (e.getSource().equals(optSelect.addRestaurantButton())){
				addRestaurant.getAddButton().addActionListener(this);
				//row3.add(findRestaurant);
				addRestaurant.setVisible(true);
			}			
			
			else if (e.getSource().equals(optSelect.editRestaurantButton())){
				editRestaurant.updateList(dL.showRestaurantNames());
				editRestaurant.getList().addListSelectionListener(this);
				editRestaurant.getEditButton().addActionListener(this);
				editRestaurant.setVisible(true);
			}
			
			else if (e.getSource().equals(optSelect.deleteRestaurantButton())){
				deleteRestaurant.updateList(dL.showRestaurantNames());
				deleteRestaurant.getDeleteButton().addActionListener(this);
				deleteRestaurant.setVisible(true);
			}

			else if (e.getSource().equals(findRestaurant.getSearchButton())){
				String foodType = findRestaurant.getFoodType();
				String zipCode = findRestaurant.getZipCode();
				Preferences prefs = findRestaurant.getPreferences();
				v = dL.findRestaurants(foodType, zipCode,prefs);
				searchResults = new SearchResultsDialog(v, dL, userLogged, adminLogged, foodType,zipCode,prefs,null);				
				searchResults.setVisible(true);				
				searchResults.getSortButton().addActionListener(this);
				if(userLogged)
					searchResults.getRateButton().addActionListener(this);
			}
			
			else if (adminLogged && e.getSource().equals(addRestaurant.getAddButton())){
				String restName = addRestaurant.getRestaurantName();
				String address = addRestaurant.getAddressName();
				String foodType = addRestaurant.getFoodType();
				String zipCode = addRestaurant.getZipCode();
				Preferences prefs = addRestaurant.getPreferences();		
				dL.addRestaurant(restName, address, zipCode, foodType, prefs);
				addRestaurant.setVisible(false);
			}	
			
			else if (adminLogged && e.getSource().equals(editRestaurant.getEditButton())){
				String restName = editRestaurant.getRestaurantName();
				String address = editRestaurant.getAddressName();
				String foodType = editRestaurant.getFoodType();
				String zipCode = editRestaurant.getZipCode();
				Preferences prefs = editRestaurant.getPreferences();		
				dL.editRestaurant(restName, address, zipCode, foodType, prefs);
				//editRestaurant.setVisible(false);
			}	
			
			else if (adminLogged && e.getSource().equals(deleteRestaurant.getDeleteButton())){
				dL.deleteRestaurant(deleteRestaurant.getSelectedRestaurant());
				deleteRestaurant.setVisible(false);
			}
			
			else if(e.getSource().equals(searchResults.getSortButton())){
				searchResults.setVisible(false);
				//System.out.println(v);
    			//System.out.println( ((String)searchResults.getSortList().getSelectedItem()).substring(0,1));
    			//System.out.println(((String)searchResults.getSortList().getSelectedItem()).substring(8,9));
    			v = dL.sortResults(v, ((String)searchResults.getSortList().getSelectedItem()).substring(0,1), 
											((String)searchResults.getSortList().getSelectedItem()).substring(8,9)); 	
				String foodType = findRestaurant.getFoodType();
				String zipCode = findRestaurant.getZipCode();
				Preferences prefs = findRestaurant.getPreferences();
				searchResults = new SearchResultsDialog(v, dL, userLogged, adminLogged, foodType,zipCode,prefs,null);
				searchResults.getSortButton().addActionListener(this);
				searchResults.getRateButton().addActionListener(this);
				searchResults.setVisible(true);	
			}
			
			else if(e.getSource().equals(searchResults.getRateButton())){
				searchResults.setVisible(false);
				//System.out.println(v);
    			//System.out.println( ((String)searchResults.getSortList().getSelectedItem()).substring(0,1));
    			//System.out.println(((String)searchResults.getSortList().getSelectedItem()).substring(8,9));
    			//v = dL.sortResults(v, ((String)searchResults.getSortList().getSelectedItem()).substring(0,1), 
				//							((String)searchResults.getSortList().getSelectedItem()).substring(8,9)); 	
				
    			 dL.rateRestaurant(searchResults.getRestName(), Integer.parseInt((String)searchResults.getRateList().getSelectedItem()));
    			
    			
    			String foodType = findRestaurant.getFoodType();
				String zipCode = findRestaurant.getZipCode();
				Preferences prefs = findRestaurant.getPreferences();
				searchResults = new SearchResultsDialog(v, dL, userLogged, adminLogged, foodType,zipCode,prefs,null);
				searchResults.getRateButton().addActionListener(this);
				searchResults.getSortButton().addActionListener(this);
				searchResults.setVisible(true);	
			}			
		}
		
	}
	
	public void windowClosing(WindowEvent e) {
		dL.getDataBase().writeUserDB();
		dL.getDataBase().closeDB();
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	// Event Listener for list of restaurants in editRestaurant
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {
            Restaurant r = dL.getDataBase().getRestaurant((String) editRestaurant.getList().getSelectedValue());
            editRestaurant.update(r);
        }
	}

		
	
}	

